package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author Psycho
 * @create 2022-04-25-16:38
 */
@Service
public class WmMaterialSerbviceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;


    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() ==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.上传图片到minIO中
        //获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //获取文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //具体文件名的拼接
        String fileName = uuid+substring;
        String path = null;
        try {
            path = fileStorageService.uploadImgFile("", fileName, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3.保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(path);
        wmMaterial.setIsCollection((short)0);
        save(wmMaterial);
        //4.返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //检查分页的数据
        dto.checkParam();
        //构建Page
        IPage page = new Page<>(dto.getPage(),dto.getSize());
        //构建查询函数
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper();
        //是否收藏
        if(dto.getIsCollection() != null && dto.getIsCollection() == 1){
            lqw.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //查询某个具体用户的
        lqw.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        //按照时间倒序
        lqw.orderByDesc(WmMaterial::getCreatedTime);
        page = page(page,lqw);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getRecords().size());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }
}
